package com.example.ListPage;

import java.util.ArrayList;
import java.util.List;

import com.example.ListPage.bean.ResultBean;

public class ListPageService<T> {

	/**
	 * 针对list进行分页处理
	 * 
	 * @author likai
	 * @param list     待分页列表
	 * @param pageSize 每页条数
	 * @param page     页数
	 * @return
	 */
	public ResultBean<T> findChildListByPage(List<T> list, int pageSize, int page) {
		ResultBean<T> result = new ResultBean<T>();
		List<T> childList = new ArrayList<T>();
		if (list == null) {
			throw new RuntimeException("null异常，不能对不存在与内存的list进行分页处理");
		}
		int count = list.size(); // 记录总数
		int pageCount = 0; // 总页数页数
		if (count % pageSize == 0) {
			pageCount = count / pageSize;
		} else {
			pageCount = count / pageSize + 1;
		}
		int startIndex = 0; // 其实索引
		int endIndex = 0; // 结束索引
		startIndex = (page - 1) * pageSize;
		if (page != pageCount) {
			endIndex = startIndex + pageSize;
		} else {
			endIndex = count;
		}
		childList = list.subList(startIndex, endIndex);
		result.setPageCount(pageCount);
		result.setChildList(childList);
		result.setTotal(count);
		return result;
	}
}
